package io.renren.modules.generator.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.generator.entity.InterviewEntity;
import io.renren.modules.generator.entity.MyResumeEntity;
import io.renren.modules.generator.entity.RecordEntity;
import io.renren.modules.generator.service.InterviewService;
import io.renren.modules.generator.service.MyResumeService;
import io.renren.modules.generator.service.RecordService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.ResumeEntity;
import io.renren.modules.generator.service.ResumeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 简历库; InnoDB free: 10240 kB
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-07-18 14:25:33
 */
@RestController
@RequestMapping("generator/resume")
public class ResumeController extends AbstractController {
    @Autowired
    private ResumeService resumeService;

    @Autowired
    private MyResumeService myResumeService;

    @Autowired
    private InterviewService interviewService;

    @Autowired
    private RecordService recordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = resumeService.queryPage(params);


        return R.ok().put("page", page);
    }

    @RequestMapping("/myList")
    public R myList(String resExpectedOccupation,Long myResState){
//        System.out.println(resExpectedOccupation);
//        System.out.println(resExpectedOccupation);
        List list=resumeService.myList(getUserId(),resExpectedOccupation,myResState);
//        System.out.println(list);

        return R.ok().put("list",list);
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{resId}")
    public R info(@PathVariable("resId") Long resId){
//        System.out.println(resId);
		List list = resumeService.getByResId(resId,getUserId());
//        System.out.println(list);
        return R.ok().put("list",list);
    }

    /*
        面试
     */
    @RequestMapping("/updateByResId")
    public R updateByResId(Long resId,Long resShare,String recImpression,String recScore,Long myResState){
        resumeService.updateResShare(resId,resShare);
        RecordEntity record=new RecordEntity();
        record.setRecHrId(getUserId());
        record.setRecResId(resId);
        record.setRecImpression(recImpression);
        record.setRecScore(recScore);
        record.setRecTime(new java.sql.Date(new java.util.Date().getTime()));
        recordService.save(record);
        myResumeService.updateResState(resId,getUserId(),myResState);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ResumeEntity resume){
       // System.out.println("preTime="+resume.getPreTime());//预约时间
        String preTime=resume.getPreTime();

        try{
            preTime = preTime.replace("Z", " UTC");
            SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date d = utcFormat.parse(preTime);
            preTime=defaultFormat.format(d);
        }catch (Exception e){
            e.printStackTrace();
        }

        Date date=new java.sql.Date(new java.util.Date().getTime());
        resume.setResHrid(getUserId());
        resume.setResTime(date);
        resume.setResFrom(1L);//自己录入
		resumeService.save(resume);
        MyResumeEntity myResume=new MyResumeEntity();
        myResume.setMyResId(resume.getResId());
        myResume.setMyTime(date);
        myResume.setMyHrId(getUserId());
        myResume.setMyResFrom(1L);//自己录入
        myResume.setMyResState(0L);//预约状态
        myResumeService.save(myResume);
        InterviewEntity interview=new InterviewEntity();
        interview.setIntwBookTime(preTime);
        interview.setIntwResumeId(resume.getResId());
        interview.setIntwHrId(getUserId());
        interview.setIntwName(resume.getResName());
        interview.setIntwMobile(resume.getResMobile());
        interviewService.save(interview);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ResumeEntity resume){
		resumeService.updateById(resume);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] resIds){
		resumeService.removeByIds(Arrays.asList(resIds));

        return R.ok();
    }

}
